package com.jiangruan.project.request;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Map;
import java.util.Set;

/**
 * @author:liangdu
 * @email:1487001474@qq.com
 * @desc:用于封装提供网络请求的功能代码，用与发送网络请求，并将网络请求的数据返回
 * @datetime: 2021/12/9 12:17
 */
public class HttpUtils {
    /**
     * 该函数用于实现get请求，并将请求的结果进行返回
     * @param path 请求的资源路径
     * @return 请求的结果
     */
    public static String get (String path){
        InputStream inputStream = null;
        InputStreamReader inputStreamReader = null;
        BufferedReader reader = null;
        String line = "";
        String result="";
        try {
            URL url = new URL(path);
            URLConnection connection = url.openConnection();
            //连接聚合数据
            connection.connect();

            //接收返回的数据，用字节流转字符流进行接收
            //字符流：按字节进行读取
            //字节流：按行读取，但只能读取文档类型
             inputStream = connection.getInputStream();
            //转换流
             inputStreamReader = new InputStreamReader(inputStream);
            //BuffereReader - 字符流
             reader = new BufferedReader(inputStreamReader);
            //while循环读取
            while ((line = reader.readLine())!=null){
                result = result+line;
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                //顺序不能换，先开的后关
          if(reader!= null){
                  reader.close();
              }
          if(inputStreamReader  !=null ){
              inputStreamReader.close();
          }
          if(inputStream!= null){
              inputStream.close();
          }
          }
            catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    /**
     * 该方法用与实现发送一个post请求 ，并将请求的解析返回
     * @param path 要请求的资源路路径
     * @param header 请求同信息
     * @param data 请求提数据
     *
     * @return 请求的响应结果
     */
    public static String post(String path, Map<String,String> header,byte[] data){
        BufferedReader reader = null;//读取响应结果的字符读取流
        OutputStream output = null;
        String result = "";//最后的结果

        try {
            URL url = new URL(path);
            HttpURLConnection coon = (HttpURLConnection) url.openConnection();
            coon.setRequestMethod("POST");
            coon.setDoInput(true);
            coon.setDoOutput(true);

            if(!header.isEmpty()){
                Set<String> keys = header.keySet();
                for (String key : keys){
                    String value = header.get(key);
                    coon.setRequestProperty(key,value);

                }
            }
            coon.connect();
            //输出流
            output = coon.getOutputStream();
            output.write(data);
            output.flush();
            //接受响应数据

            reader = new BufferedReader(new InputStreamReader((coon.getInputStream())));
            String line = null;
            while ((line = reader.readLine())!= null){
                result += line;
            }



        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
            if (reader!= null){
                    reader.close();
                }
            if (output != null){
                output.close();
                }
            }catch (IOException e) {
                e.printStackTrace();
            }
        }
        return  result;
    }
}
